/**
 * 更新问题
 */
import query from '../../untils/mysql';
import { Sql, Tip } from '../../untils/untils';
import { check, validationResult } from 'express-validator';
import mydayjs from '../../untils/day';
import * as uuid from 'uuid';

const validator = [
    check('id').notEmpty().withMessage("问题ID不能为空！")
]
const POST = async (req: any, res: any, next: any) => {
    try {
        const { id, remarks, actionStatus } = req.body;
        const { userId } = req.auth;
        var errors = validationResult(req);
        if (!errors.isEmpty()) {
            return Tip.tipError(res, errors.array()[0].msg);
        }
        if (actionStatus) {
            req.body.actionTime = mydayjs(new Date())
        }
        let set = Sql.set(req.body);
        if (!set) {
            return Tip.tipError(res, "请传入需求修改的值");
        }
        const data = await query(`select * from problem where id = '${id}'`);
        if (data.length === 0) {
            return Tip.tipError(res, "当前项目不存在");
        }

        const sql = `update problem set ${set} where id = '${id}'`;
        await query(sql)
        if (actionStatus) {
            await query(`insert into problem_move (id,userId,remarks,actionStatus,problemId,projectId) values ('${uuid.v1()}','${userId}','${remarks}','${actionStatus}','${id}','${data[0].project_id}')`)
        }
        return Tip.tipSuccess(res, '更新成功')
    } catch (error: any) {
        Tip.tipError500(res, error)
    }
}

export {
    POST,
    validator
}